package com.example.monit.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;

public class TimeUtils {

    private static final HashMap<String, ChronoUnit> typeMap = new HashMap<>(6);

    static {
        typeMap.put("s", ChronoUnit.SECONDS);
        typeMap.put("m", ChronoUnit.MINUTES);
        typeMap.put("h", ChronoUnit.HOURS);
        typeMap.put("d", ChronoUnit.DAYS);
        typeMap.put("M", ChronoUnit.MONTHS);
        typeMap.put("y", ChronoUnit.YEARS);
    }

    /**
     * 根据 时间单位返回单位对应的毫秒枚举
     * @param type
     * @return
     */
    public static ChronoUnit getType(String type){
        ChronoUnit unit = typeMap.getOrDefault(type, ChronoUnit.DAYS);
        return unit;
    }


    /**
     * 毫秒装 LocalDateTime
     *
     * @param timestamp 时间戳
     * @return
     */
    public static LocalDateTime timestamToDatetime(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        return LocalDateTime.ofInstant(instant, ZoneId.of("+0"));
    }

}
